package com.org.cys.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.org.cys.util.SpringUtils;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.Assert;


/**
 * JSON格式转换工具类
 * @author caiyushen
 * */
public final class JsonUtils {
    private static ObjectMapper objMapper = null;

    private JsonUtils() {
    }

    /**
     * 对象转json字符串
     * @param value
     * */
    public static String toJson(Object value) {
        try {
            return getObjMapper().writeValueAsString(value);
        } catch (JsonProcessingException var2) {
            throw new RuntimeException(var2);
        }
    }

    
    /**
     * json串转对象
     * @param json - json串
     * @param valueType - 对象类型
     * */
    public static <T> T toObject(String json, Class<T> valueType) {
        Assert.hasText(json);
        Assert.notNull(valueType);

        try {
            return getObjMapper().readValue(json, valueType);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    
    /**
     * json串转对象
     * @param json - json串
     * @param typeReference - 对象类型
     * */
    public static <T> T toObject(String json, TypeReference<?> typeReference) {
        Assert.hasText(json);
        Assert.notNull(typeReference);

        try {
            return getObjMapper().readValue(json, typeReference);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    
    /**
     * json串转对象
     * @param  json - json串
     * @param  javaType - 对象类型
     * */
    public static <T> T toObject(String json, JavaType javaType) {
        Assert.hasText(json);
        Assert.notNull(javaType);

        try {
            return getObjMapper().readValue(json, javaType);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    /**
     * json串转具有指定泛型的List对象
     * @param json - json串
     * @param valueType - 对象类型
     * */
    public static List toList(String json, Class<?> valueType) {
        Assert.hasText(json);
        Assert.notNull(valueType);
        JavaType javaType = getObjMapper().getTypeFactory().constructParametricType(ArrayList.class, new Class[]{valueType});

        try {
            return (List)getObjMapper().readValue(json, javaType);
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }

    
    /**
     * 将对象转换为json流
     * @param  writer - writer
     * @param  value - 对象
     * */
    public static void writeValue(Writer writer, Object value) {
        try {
            getObjMapper().writeValue(writer, value);
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }

    private static ObjectMapper getObjMapper() {
        if(objMapper == null) {
            try {
                objMapper = (ObjectMapper)SpringUtils.getBean("jsonDataMapper");
            } catch (Exception var1) {
                ;
            }
        }
        return objMapper;
    }
}
